package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.CheckinSetmealDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author maochuan
 */
@RestController
@RequestMapping("CheckinSetmeal")
public class CheckinSetmealController extends BaseController {

    @Autowired
    @Qualifier("CheckinSetmealServiceImplOne")
    private CheckinSetmealService checkinSetmealService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("NursingLevelServiceImplOne")
    private NursingLevelService nursingLevelService;

    @Autowired
    @Qualifier("RoomTypeServiceImplOne")
    private RoomTypeService roomTypeService;


    @Autowired
    @Qualifier("CheckinServiceImplOne")
    private CheckinService checkinService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            String nursingLevelCode = jsonObject.getString("nursingLevelCode");
            String roomTypeCode = jsonObject.getString("roomTypeCode");
            double univalence = jsonObject.getDouble("univalence");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinSetmeal/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            CheckinSetmeal checkinSetmeal = checkinSetmealService.getByInstitutionCodeAndName(institutionCode,name);
            if(checkinSetmeal!=null){
                return failed("此套餐已存在");
            }
            NursingLevel nursingLevel = nursingLevelService.getByNursingLevelCode(nursingLevelCode);
            if(nursingLevel==null){
                return failed("此护理级别不存在");
            }
            RoomType roomType = roomTypeService.getByRoomTypeCode(roomTypeCode);
            if(roomType==null){
                return failed("房间类型不存在");
            }

            checkinSetmeal = new CheckinSetmeal();
            checkinSetmeal.setCheckinSetmealCode(UUID.randomUUID().toString());
            checkinSetmeal.setName(name);
            checkinSetmeal.setNursingLevelCode(nursingLevelCode);
            checkinSetmeal.setNursingLevelName(nursingLevel.getName());
            checkinSetmeal.setNursingLevelPrice(nursingLevel.getPriceMonth());
            checkinSetmeal.setRoomTypeCode(roomTypeCode);
            checkinSetmeal.setRoomTypePrice(roomType.getPriceMonth());
            checkinSetmeal.setStatus(0);
            checkinSetmeal.setCreateTime(new Date());
            checkinSetmeal.setUnivalence(univalence);
            checkinSetmeal.setStaffCode(staffCode);
            checkinSetmeal.setInstitutionCode(institutionCode);
            checkinSetmealService.insert(checkinSetmeal);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinSetmeal/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "IsNotEdit", method = RequestMethod.POST)
    public MyResult IsNotEdit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String checkinSetmealCode = jsonObject.getString("checkinSetmealCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinSetmeal/IsNotEdit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            boolean flag  = true;
            List<Checkin> checkinList = checkinService.getByCheckinSetmealCodeAndStatus1(checkinSetmealCode);
            if(checkinList.size()>0){
                flag = false;
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("isNotEdit",flag);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/CheckinSetmeal/IsNotEdit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult edit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            String nursingLevelCode = jsonObject.getString("nursingLevelCode");
            String roomTypeCode = jsonObject.getString("roomTypeCode");
            int status = jsonObject.getInteger("status");
            double univalence = jsonObject.getDouble("univalence");
            String checkinSetmealCode = jsonObject.getString("checkinSetmealCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinSetmeal/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            CheckinSetmeal checkinSetmeal = checkinSetmealService.getByInstitutionCodeAndName(institutionCode,name);
            if(checkinSetmeal!=null){
                if(!checkinSetmeal.getCheckinSetmealCode().equals(checkinSetmealCode)){
                    return failed("此套餐已存在");
                }
            }
            checkinSetmeal = checkinSetmealService.getByCheckinSetmealCode(checkinSetmealCode);

            if (checkinSetmeal == null) {
                return failed("数据不存在");
            }
            NursingLevel nursingLevel = nursingLevelService.getByNursingLevelCode(nursingLevelCode);
            if(nursingLevel==null){
                return failed("此护理级别不存在");
            }
            RoomType roomType = roomTypeService.getByRoomTypeCode(roomTypeCode);
            if(roomType==null){
                return failed("房间类型不存在");
            }

            checkinSetmeal.setName(name);
            checkinSetmeal.setNursingLevelCode(nursingLevelCode);
            checkinSetmeal.setNursingLevelName(nursingLevel.getName());
            checkinSetmeal.setNursingLevelPrice(nursingLevel.getPriceMonth());
            checkinSetmeal.setRoomTypeCode(roomTypeCode);
            checkinSetmeal.setRoomTypePrice(roomType.getPriceMonth());
            checkinSetmeal.setStatus(status);
            checkinSetmeal.setUnivalence(univalence);

            checkinSetmealService.updateByCheckinSetmealCode(checkinSetmeal);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinSetmeal/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins列表
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/CheckinSetmeal/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<CheckinSetmeal> checkinSetmealPageInfo = checkinSetmealService.getPageList(pageIndex, pageSize, institutionCode);

            List<CheckinSetmealDTO> checkinSetmealDTOList = new ArrayList<>();
            for (CheckinSetmeal checkinSetmeal:checkinSetmealPageInfo.getList()){
                CheckinSetmealDTO checkinSetmealDTO = mapper.map(checkinSetmeal,CheckinSetmealDTO.class);
                checkinSetmealDTOList.add(checkinSetmealDTO);
            }
            for(CheckinSetmealDTO checkinSetmealDTO:checkinSetmealDTOList){
                NursingLevel nursingLevel = nursingLevelService.getByNursingLevelCode(checkinSetmealDTO.getNursingLevelCode());
                checkinSetmealDTO.setNursingLevelName(nursingLevel.getName());
                RoomType roomType = roomTypeService.getByRoomTypeCode(checkinSetmealDTO.getRoomTypeCode());
                checkinSetmealDTO.setRoomTypeDTO(roomType);
            }
            checkinSetmealPageInfo.setList(Collections.unmodifiableList(checkinSetmealDTOList));

            return success(checkinSetmealPageInfo);
        } catch (Exception e) {
            insertError("/CheckinSetmeal/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //gov列表
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/CheckinSetmeal/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<CheckinSetmeal> checkinSetmealPageInfo = checkinSetmealService.pageByGovCode(pageIndex, pageSize,
                    staff.getGovernmentCode());

            List<CheckinSetmealDTO> checkinSetmealDTOList = new ArrayList<>();
            for (CheckinSetmeal checkinSetmeal:checkinSetmealPageInfo.getList()){
                CheckinSetmealDTO checkinSetmealDTO = mapper.map(checkinSetmeal,CheckinSetmealDTO.class);
                checkinSetmealDTOList.add(checkinSetmealDTO);
            }
            for(CheckinSetmealDTO checkinSetmealDTO:checkinSetmealDTOList){
                NursingLevel nursingLevel = nursingLevelService.getByNursingLevelCode(checkinSetmealDTO.getNursingLevelCode());
                checkinSetmealDTO.setNursingLevelName(nursingLevel.getName());
                RoomType roomType = roomTypeService.getByRoomTypeCode(checkinSetmealDTO.getRoomTypeCode());
                checkinSetmealDTO.setRoomTypeDTO(roomType);
            }
            checkinSetmealPageInfo.setList(Collections.unmodifiableList(checkinSetmealDTOList));

            return success(checkinSetmealPageInfo);
        } catch (Exception e) {
            insertError("/CheckinSetmeal/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //查询全部
    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult list(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/CheckinSetmeal/List");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            List<CheckinSetmeal> checkinSetmealList = checkinSetmealService.getListAll(institutionCode);
            List<CheckinSetmealDTO> checkinSetmealDTOList = new ArrayList<>();
            for (CheckinSetmeal checkinSetmeal:checkinSetmealList){
                CheckinSetmealDTO checkinSetmealDTO = mapper.map(checkinSetmeal,CheckinSetmealDTO.class);
                checkinSetmealDTOList.add(checkinSetmealDTO);

            }
            for(CheckinSetmealDTO checkinSetmealDTO:checkinSetmealDTOList){
                NursingLevel nursingLevel = nursingLevelService.getByNursingLevelCode(checkinSetmealDTO.getNursingLevelCode());
                checkinSetmealDTO.setNursingLevelName(nursingLevel.getName());
                RoomType roomType = roomTypeService.getByRoomTypeCode(checkinSetmealDTO.getRoomTypeCode());
                checkinSetmealDTO.setRoomTypeDTO(roomType);
            }

            return success(checkinSetmealDTOList);

        } catch (Exception e) {
            insertError("/CheckinSetmeal/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
